<?php
session_start();

//$email = $_SESSION['email'];
$email = 'a@pdn.ac.lk';

include 'admin/config.php';

$connetion = mysql_connect($db_host, $db_username, $db_password) or die('Error in DB');
$db = mysql_select_db($db_name) or die('Érror in selecting db');

$default_combo = "";

if ($_GET['get_times']) {
	$str_dates = generate_days($_GET['day']);
	$day = $_GET['day'];
	$str_times = generate_times($day, $_GET['time']);
}
elseif ($_GET['get_list']) {
	//Populate the combo boxes
	$str_dates = generate_days($_GET['day']);
	$str_times = generate_times($day, $_GET['time']);
	
	//Get the list of subjects that fall in to the criteria
	$str_exams = generate_subjects($_GET['day'], $_GET['time']);	
}
elseif ($_GET['accept']){
	
	//Add to database
	$query = "insert into supervises_subject values ('" . $_GET['select_exam'] . "', '$email')";
	$result = mysql_query($query);
	
	echo "query ". $query;
	
	$tmp = explode(' ', $_GET['select_exam']);
	$sub_code = strtolower($tmp[0]);
	
	$query = "insert into supervises_exam (Email, Sub_Code) values ('$email', '$sub_code')";
	//echo $query;
	$result = mysql_query($query);
	
	//Load the combo box, default value is last selected
	$default_combo = $_GET['day'];
	
	$query = 'select * from Exam';
	$result = mysql_query($query);

	$dates = array();
	
	while ($row = mysql_fetch_assoc($result)){
		$tmp = explode(" ", $row['Start_Time']);
		$dates[] = $tmp[0];
	}
	
	$dates = array_unique($dates);
	sort($dates);
	
	$str_dates = "<option value=\"".$default_combo."\">".$default_combo."</option>";

	foreach ($dates as $v) {
		$str_dates =  $str_dates."<option value=\"".$v."\">".$v."</option>";
	}
	
	$query = "select * from Exam where Start_Time > '".$default_combo." 00:00:00' and Start_Time < '".$default_combo." 23:59:59'";
	$result = mysql_query($query);
	
	$sub_array = array();
	
	while ($row = mysql_fetch_assoc($result)){
        $sub_array[] = $row['Sub_Code'];
    }
	
	$str_exams = "";
	
	foreach ($sub_array as $v) {
        $str_exams =  $str_exams."<option value=\"".$v."\">".$v."</option>";
    }
}
else {
	$str_dates = generate_days($_GET['day']);
}

function generate_days($start_day = NULL){
	//Just load the first combo box
	$query = 'select distinct sub_day from sase_subject';
	$result = mysql_query($query);

	$dates = array();

	while ($row = mysql_fetch_assoc($result)){
		$dates[] = $row['sub_day'];
	}
	
	$str_dates = "";

	foreach ($dates as $v) {
		$str_dates =  $str_dates."<option value=\"".$v."\">".$v."</option>" . PHP_EOL;
	}
	
	if ($start_day) $str_dates = "<option value=\"".$start_day."\">".$start_day."</option>" . PHP_EOL . $str_dates;
	
	return $str_dates;
}

function generate_times($day, $start_time = NULL) {
	//times will hold the particular time values for the exam
	$times = array();
	
	//Get values of times for the given day
	$query = "select sub_start, sub_end from sase_subject where sub_day = '$day'";
	$result = mysql_query($query);
	
	//put them in to the times array
	while ($row = mysql_fetch_assoc($result)) {
		$times[] = $row['sub_start'] . ' to ' . $row['sub_end'];	
	}
	
	$times = array_unique($times);
	sort($times);
	
	//generate option box optins html
	$str_times = "";
	foreach ($times as $v) {
        $str_times =  $str_times."<option value=\"".$v."\">".$v."</option>" . PHP_EOL;
    }
	
	if ($start_time) 
		$str_times = "<option value=\"".$start_time."\">".$start_time."</option>" . PHP_EOL . $str_times;
		
	return $str_times;
}

function generate_subjects ($day, $time) {
	$times = explode(' to ', $time);
	$query = "select * from sase_subject where sub_day = '$day' and sub_start >= '" . $times[0] . "' and sub_end <= '" . $times[1] . "' order by sub_start";
	echo $query;
	$result = mysql_query($query);
	
	$sub_str = "";
	while ($row = mysql_fetch_assoc($result)) {
		$sub_str = $sub_str . "<option value=\"" . $row['sub_code'] . "\">" . $row['sub_code'] . ' : ' . $row['sub_name'] . ' : ' . $row['sub_start'] . ' to ' . $row['sub_end'] . "</option>";
	}
	return $sub_str;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome <?php echo $var_supname; ?></title>
<style type="text/css">
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	background-color: #CCC;
}

</style>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>



<body>

<div class="container">
	<div class="headder">
   	  &nbsp;
    </div>
  <div class="menu_bar">
    	<a href="index.php">Home</a> |
        <a href="">Logout</a> |
        <a href="">Change Password</a> |
        <a href="">Contact Admin</a>
</div>
  
	<div class="sitebody">
    	<div class="left">
        
       	  <p>Select preferable exams...</p>
    	  
          <div class="search_box">
          	
            <form action="add.php" method="get">
            	<center>
                <h4>Preferable Day</h4>
                <select name="day" size="1">
                    <?php echo $str_dates; ?>
                </select>
                <input type="submit" value="get times" name="get_times" />			
            	</center>
            </form>
        </div>
        
        <div class="search_box">
            <form action="add.php" method="get">
            	<center>
                	<h4>Preferable Time</h4>
                  <select name="time" size="1" id="time">
                        <?php echo $str_times; ?>
                    </select>
                  <input type="hidden" value="<?php echo $day; ?>" name="day" />
                    <input type="submit" value="get list" name="get_list" />
             	</center>
            </form>
    	</div>
        
        <div class="search_box">
        	
            <form action="add.php" method="get">
                <center>
                <h4>Preferable Subject</h4>
                <select name="select_exam" size="1">
                    <?php echo $str_exams; ?>
                </select>
                <input type="submit" value="accept" name="accept" />
            	</center>
            </form>
        </div>
        
        <div class="search_box">
            <center><a href="index.php">Back to Home</a></center>
        </div>
          
   	  </div>
    	<div class="right">
       	  <div class="profile_image">Profile Image Over Here</div>
          <div style="clear:both;"></div>
          <div class="statistics">
          	<h4>Welcome <?php echo $user_name; ?></h4>
            <table width="100%" border="0" style="font-size:12px;">
              <tr>
                <td width="43%" align="right">Allocations : </td>
                <td width="57%"><?php echo $html_statistics_count; ?></td>
              </tr>
              <tr>
                <td align="right">Start Day :</td>
                <td><?php echo $var_start_day; ?>2011-04-20</td>
              </tr>
              <tr>
                <td align="right">End Date :</td>
                <td><?php echo $var_end_day; ?>2011-04-29</td>
              </tr>
            </table>

          </div>
        </div>
    </div>
    <div style="clear:both;"></div>
    <div class="footer">
    
    </div>
</div>


</body>
</html>